import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:speed_random/colors_data.dart';
import 'package:speed_random/common/common_fuc.dart';
import 'package:speed_random/global_data.dart';
import 'package:speed_random/widgets/zdy_vip_bottom_tips.dart';


// 添加选择音效选择弹窗方法



Future<void> zdySelectMp3Confirm(
    BuildContext context,
    String currentSound,
      Function(String mp3path,String mp3name) onSoundSelected, {
      List? mp3s,
      String dialogTitle = '选择音效',
    }) async {
  final Mp3List = mp3s ?? colorsData.getSound();
  String selectedSound = currentSound; // 添加临时变量保存当前选择
  String selectedSoundname = ""; // 添加临时变量保存当前选择

  // ... 其他代码保持不变 ...
  void playSound(String soundPath) async {

    commonFuc.playMp3(path: soundPath);
  }


  await showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      builder: (BuildContext context) {
        return StatefulBuilder(
            builder: (context, setState) {
              return Container(
                height: MediaQuery.of(context).size.height * 0.8,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
                ),
                padding: EdgeInsets.all(16),
                child: Column(
                  children: [
                    Container(
                      width: 40,
                      height: 4,
                      margin: EdgeInsets.only(bottom: 16),
                      decoration: BoxDecoration(
                        color: Colors.grey[300],
                        borderRadius: BorderRadius.circular(2),
                      ),
                    ),
                    Text(
                      dialogTitle,
                      style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold,color: Colors.black),
                    ),
                    SizedBox(height: 16),
                Expanded(
                  child: ListView.builder(
                   // shrinkWrap: true,
                    itemCount: Mp3List.length,
                    itemBuilder: (context, index) {

                      var isvip= globalData.userInfo!.isvip==false &&  Mp3List[index]['path']!.indexOf("vip")>-1 ;


                      return Container(
                        margin: EdgeInsetsGeometry.directional(bottom: 10),
                          decoration: BoxDecoration(
                            color: selectedSound == Mp3List[index]['path']
                                ? Colors.blue.withOpacity(0.2)
                                : Colors.grey.withOpacity(0.1),
                            borderRadius: BorderRadius.circular(8),
                            border: Border.all(
                              color: selectedSound == Mp3List[index]['path']
                                  ? Colors.blue
                                  : Colors.transparent,
                              width: 2,
                            ),
                          ),
                          child: ListTile(
                        title: Text(Mp3List[index]['name']!,style: TextStyle(color: Colors.black),),
                        trailing: isvip?Icon(Icons.lock,color: Colors.red,):null,
                        onTap: (){



                          playSound(Mp3List[index]['path']!);
                          setState(() {
                            selectedSound = Mp3List[index]['path']!;
                            selectedSoundname = Mp3List[index]['name']!;
                          });
                        },
                      ));
                    },
                  ),
                ),
                SizedBox(height: 16),
                ElevatedButton(
                  onPressed: () {
                    if(selectedSound.indexOf("vip")>-1){
                      zdyVipBottomTips();
                      return ;
                    }


                    Navigator.of(context).pop();
                    onSoundSelected(selectedSound, selectedSoundname);

                  },
                  child: Text('确认选择'.tr),
                ),
              ],
            ),

        );
      }
      );

      }
  );
}